;(function($){
	var os=detectOS();
	//------------判断浏览器、操作系统
	function detectOS() {
		var	userAgent=navigator.userAgent;
		//console.log(userAgent);
		var os = {};
		os.android = userAgent.match(/(Android)\s+([\d.]+)/) || userAgent.match(/Silk-Accelerated/) ? true : false;
		os.androidICS = os.android && userAgent.match(/(Android)\s4/) ? true : false;
		os.ipad = userAgent.match(/(iPad).*OS\s([\d_]+)/) ? true : false;
		os.iphone = !os.ipad && userAgent.match(/(iPhone\sOS)\s([\d_]+)/) ? true : false;
		os.ios = os.ipad || os.iphone;
		os.wp=userAgent.match(/Windows Phone/) || userAgent.match(/IEMobile/) ? true : false;
		os.supportsTouch = ((window.DocumentTouch && document instanceof window.DocumentTouch) || 'ontouchstart' in window);
		return os;
	}
	if(os.android || os.iphone || os.wp || os.androidICS || os.ipad){
		if(os.ipad){
			var turnBox = $('<div id="turnBox"><span><img src="assets/images/turn.png" /></span></div>');
			//window.orientation = 0;
	        function orient() {
	            if (window.orientation == 0 || window.orientation == 180) { //竖屏提示
	                $("body").addClass('portrait').removeClass('landscape');
	                orientation = 'portrait';
	                turnBox.appendTo('body');
	                return false;
	            }else if (window.orientation == 90 || window.orientation == -90) {
	                $("body").addClass('landscape').removeClass('portrait');
	                orientation = 'landscape';
	                turnBox.remove();
	                return false;
	            }
	        }
	        orient();
	        $(window).on( 'orientationchange', function(e){
	            orient();
	        });
		}else{
			window.location.href = 'mobile/index.html';
		}
		
	};
	
	var side_menu = {};
	$('#menu').each(function(index,item){
		var openBL = false;
		var opbg = $('<div class="menu_bg"></div>');
		opbg.hide().appendTo('body');
		side_menu.open = function(){
			openBL = true;
			opbg.show();
			$(item).animate({
					left: 0
			}, {duration: 500, easing: 'easeOutQuart',complete: function(){
				$(item).addClass("open");
			}}).css({zIndex:999});
			var s=s_gi(s_account);
			s.linkTrackVars="events";
			s.linkTrackEvents="event5";
			s.events="event5";
			s.tl(this,'o','custom');
		};
		side_menu.close = function(){
			openBL = false;
			$(item).animate({
					left: '-15%'
			}, {duration: 500, easing: 'easeInQuart',complete: function(){
				opbg.hide();
				$(item).removeClass("open");
			}}).css({zIndex:99});
		};
		$(item).find('.menu_tigger').on('click',function(){
			if(!openBL){
				side_menu.open();
			}else{
				side_menu.close();
			};
		});
		opbg.click(function(){
			side_menu.close();
		});
		$(this).find('.menu_list li').each(function(){
			$(this).on('mouseover',function(){
				$(this).addClass('active').siblings('li').removeClass('active');
			});
			$(this).on('mouseleave',function(){
				$(this).removeClass('active');
			});
		});
	});
	$(window).resize(function(event) {
		$('.fc_list li').each(function(index,item){
			$(item).find('.img_wrap').each(function(ind,itm){
				$(itm).css({marginTop: -$(itm).height()/2});
			});
		});
		layoutFn();
		try {
			videoStart();
		}catch(error){
			//console.log(error);
		};
	});
	(function(){
		var manifest = ["assets/images/lib0_img_000001.png","assets/images/lib0_img_000002.png","assets/images/lib1_img_000001.png","assets/images/lib1_img_000002.png","assets/images/lib3_img_000002.png","assets/images/lib3_img_000003.png"];
		$("#main_content img").each(function(){
			manifest.push($(this).attr('src'));
		});
		var handleProgress = function(e) {
			var p = Math.round(e.progress * 100);
			$('.loading_page>li').each(function(index,item){
				if(e.progress < (index+1)/$('.loading_page>li').length){
					$(item).show().siblings().hide();
					return false;
				};
			});
			$('.loading_bar').width(p+'%');
			$('#loading .loading_txt span').text(p + '%');
		} //加载时回调
		var handleComplete = function(e) {
			$('.loading_bar').width('100%');
			$('#loading').remove();
			$('#main_content').show();
			if($('.fc_list').length){
				side_menu.open();
				setTimeout(function(){
					side_menu.close();
				},3000);
			}
			$(window).resize();
		} //加载完毕回调
		if(manifest.length>0){
			var preload = new createjs.LoadQueue(false);//false会缓存文件，二次点击不会重新请求，true每次点击都会重新请求文件
			preload.on("progress", handleProgress, this);
			preload.on("complete", handleComplete, this);
			preload.loadManifest(manifest);
		}else{
			handleComplete();
		};
	})();
	
	function layoutFn(){
		var viewculm = $('.viewboard .view_culm'),
			vw = viewculm.parent().width(),
			normal_vw = '25%',
			max_vw = '50%', //vw/2
			min_vw = 1/4, //max_vw/3
			st,
			ww = $('.main_content_inner').width(),
			wh = $(window).height(),
			canvascale = ww*0.375/800,
			xx = 800-ww*0.375;
		if(wh <= 768){
			$('body').addClass('small');
		}else{
			$('body').removeClass('small');
		};

		var code_arr = [
			'脱口秀场PC端',
			'说唱达人PC端',
			'舌尖食神PC端'
		];
		TweenLite.set($(".canvasView"), {x:-xx/2,transformOrign:'0% 0%', scale:canvascale});
		function eventFn(item,event){
			$('.canvas_wrap').each(function(ind,itm){
				if(ind == $(item).index()){
					s.pageName=code_arr[ind]; 
					s.channel=code_arr[ind];
					s.t();
					var ss=s_gi(s_account);
					ss.linkTrackVars="events";
					ss.linkTrackEvents="event"+(ind+8);
					ss.events="event"+(ind+8);
					ss.tl(this,'o','custom');
					$(itm).find('.canvasView')[0].exportRoot.show();
					$(itm).find('.canvasView')[0].exportRoot.onPlay();
					TweenLite.to($(itm).find('canvas'), 0.5, {margin:'0'});
				}else{
					$(itm).find('.canvasView')[0].exportRoot.hidden();
					$(itm).find('.canvasView')[0].exportRoot.onStop();
					TweenLite.to($(itm).find('canvas'), 0.5, {marginLeft:'60%'});
				};
			});
			var current = TweenLite.to($(item), 0.5, {width:max_vw});
			current.eventCallback("onComplete", function(e){
				$(item).css("cursor", "default").addClass('active');
			},[]);
			var paused = TweenLite.to($(item).siblings('.view_culm'), 0.5, {width:min_vw*100+'%'});
			paused.eventCallback("onComplete", function(e){
				$(item).css("cursor", "pointer").removeClass('active');
			},[]);
			$(item).find('.doll_icon_wrap').fadeOut('200', function() {
				
			});
			$(item).siblings('.view_culm').find('.doll_icon_wrap').fadeIn('500', function() {});
		};
		function eventFnEv(item,event){
			$('.canvas_wrap').each(function(ind,itm){
				$(itm).find('.canvasView')[0].exportRoot.show();
				$(itm).find('.canvasView')[0].exportRoot.onPlay();
				$(itm).find('.canvasView')[0].exportRoot.hideN();
				TweenLite.to($(itm).find('canvas'), 0.5, {marginLeft:'60%'});
			});
			var current = TweenLite.to($(item), 0.5, {width:33.3333+'%'});
			current.eventCallback("onComplete", function(e){
				$(item).css("cursor", "default").removeClass('active');
			},[]);
			var paused = TweenLite.to($(item).siblings('.view_culm'), 0.5, {width:33.3333+'%'});
			$(item).find('.doll_icon_wrap').fadeIn('500', function() {});
		};
		function action(item){
			
			$(item).off('mouseenter').on('mouseenter',function(event){
				eventFn(item,event);
				event.stopPropagation();
			});
			$(item).off('mouseleave').on('mouseleave',function(event) {
				clearTimeout(st);
				eventFnEv(item,event)
				event.stopPropagation();
			});
		};
		viewculm.each(function(index,item){
			action(item);
		});
	};
	
	function canvasModel(){
		this.init();
	};
	canvasModel.prototype = {
		loadCanvas: function(){
			var sf = this;
			function addEvent(index,item,canvas,images,manifest,loader){
				var self = this;
				var images = images||{};
				loader.addEventListener("fileload", handleFileLoad);
				loader.addEventListener("complete", handleComplete);
				
				loader.loadManifest(manifest);
				function handleFileLoad(evt) {
					if (evt.item.type == "image") { images[evt.item.id] = evt.result; }
				};
				function handleComplete() {
					var exportRoot = new lib['canvas'+index]();
					stage = new createjs.Stage(canvas);
					stage.addChild(exportRoot);
					stage.update();
					createjs.Ticker.setFPS(30);
					createjs.Ticker.addEventListener("tick", stage);
					canvas.exportRoot = exportRoot;
					exportRootClick(exportRoot);
				};
				function exportRootClick(exportRoot){
					$(exportRoot.instance.instance_1).off('click').on('click',function(event) {
						//console.log(index)
						var s=s_gi(s_account);
						s.linkTrackVars="events";
						s.linkTrackEvents="event"+(index+19);
						s.events="event"+(index+19);
						s.tl(this,'o','custom');
						window.location.href = 'video.html?page='+(index+1);
					});
				}
				
			};
			this.canvasArr.each(function(index,item){
				var canvas = item;
				var manifest = sf.manifest[index];
				var loader = new createjs.LoadQueue(false);
				addEvent(index,item,canvas,images,manifest,loader);
			});
		},
		init: function(){
			this.canvasArr = $('.canvasView');
			this.imageObj = this.imageObj||{};
			this.manifest = [
				[
					{src:"assets/images/lib0_img_000001.png", id:"lib0_img_000001"},
					{src:"assets/images/lib0_img_000002.png", id:"lib0_img_000002"},
					{src:"assets/images/name1.png", id:"name1"}
				],
				[
					{src:"assets/images/lib1_img_000001.png", id:"lib1_img_000001"},
					{src:"assets/images/lib1_img_000002.png", id:"lib1_img_000002"},
					{src:"assets/images/name2.png", id:"name2"}
				],
				[
					{src:"assets/images/lib3_img_000002.png", id:"lib3_img_000002"},
					{src:"assets/images/lib3_img_000003.png", id:"lib3_img_000003"},
					{src:"assets/images/name3.png", id:"name3"}
				]
			];
			// [
				// 	{src:"assets/images/lib2_img_000001.png", id:"lib2_img_000001"},
				// 	{src:"assets/images/lib2_img_000002.png", id:"lib2_img_000002"}
				// ],
			this.loadCanvas();
		}
	};
	new canvasModel();
	$('#menu_list').each(function(){
		$(this).html('');
		$('<li><a class="link_index" href="index.html"><img src="assets/images/menu_icon_1.png" /></a></li><li><a class="btn_rule"><img src="assets/images/menu_icon_2.png" /></a></li><li><a class="link_show" href="show.html"><img src="assets/images/menu_icon_3.png" /></a></li><li><a class="btn_winlist"><img src="assets/images/menu_icon_4.png" /></a></li><li><a class="link_media" href="media.html"><img src="assets/images/menu_icon_5.png" /></a></li><li><a target="_blank" href="http://www.skoda.com.cn/fabia/index.html" class="link_product"><img src="assets/images/menu_icon_6.png" /></a></li><li><a class="link_testdriver" target="_blank" href="http://www.skoda.com.cn/fabia/index.html?open=testDriver"><img src="assets/images/menu_icon_7.png" /></a></li>').appendTo($(this));
		var rule_content = $('<div class="rule_content"><div class="rule_tit"><img src="assets/images/rule_tit.png" /></div><div class="content"><div class="rule_scroll_wrap"><div class="img"><img src="assets/images/rule_content_img.png" /></div><img class="block" src="assets/images/rule_content.png" /></div></div></div>');
		var win_content = $('<div class="win_content"><div class="win_tit"><img src="assets/images/win_tit.png" /></div><div class="content"><div class="win_scroll_wrap"><img  class="block" src="assets/images/win_img.png" /></div></div></div>');
		$('.btn_rule').on('click',function(){
			side_menu.close();
			$.fancybox.open(rule_content,{
				wrapCSS : 'own_skin rule_skin',
				padding : 0,
				afterShow: function(){
					$('.rule_scroll_wrap').jScrollPane();
				},
				afterClose: function(){

				}
			});
		});
		$('.btn_winlist').on('click',function(){
			side_menu.close();
			$.fancybox.open(win_content,{
				wrapCSS : 'own_skin win_skin',
				padding : 0,
				afterShow: function(){
					$('.win_scroll_wrap').jScrollPane();
				},
				afterClose: function(){

				}
			});
		});
	});
	
	$('.share_icon_weixin').each(function(){
		var pop = $('<div class="pop qrcode_pop"><div class="qrcode_pop_inner"><img src="assets/images/share_info_1.png" /></div></div>');
		$(this).on('mouseover',function(){
			var s=s_gi(s_account);
			s.linkTrackVars="events";
			s.linkTrackEvents="event24";
			s.events="event24";
			s.tl(this,'o','custom');
			pop.prependTo(this);
		});
		$(this).on('mouseout',function(){
			pop.remove();
		});
	});
	var share_sina_arr = [
		{
			'tit':'脱口秀一哥',
			'content':'“快乐真人售”开场啦~我围观了脱口秀一哥Mike隋，欢乐着把车卖了也真是够绝的。还有说唱达人、舌尖食神……马上围观点赞有机会赢索尼PS4大奖呦！#全新晶锐# #只为快乐#',
			'pic': 'pos1.jpg'
		},
		{
			'tit':'说唱达人',
			'content':'“快乐真人售”开场啦~ yo yo,check it out！王厂长用rap来秀车真是帅呆了！还有舌尖食神、脱口秀一哥……马上围观点赞还能赢取索尼PS4大奖！#全新晶锐# #只为快乐#',
			'pic': 'pos2.jpg'
		},
		{
			'tit':'舌尖食神',
			'content':'“快乐真人售”开场啦~吃货妹子量身定制《舌尖上的晶锐》，这种混搭真是酸爽~还有说唱达人、脱口秀一哥……马上围观点赞有机会赢索尼PS4大奖呦！#全新晶锐# #只为快乐#',
			'pic': 'pos3.jpg'
		}
	];
	$('.share_icon_sina').each(function(index,item){
		s.tl(this,'o','custom');
		var arr = share_sina_arr;
		var share_link = 'http://service.weibo.com/share/share.php?url=http://www.fabiachina.com/allforhappiness/video.html?page='+(index+1)+'&title='+encodeURIComponent(arr[index]['content'])+'&pic=http://www.fabiachina.com/allforhappiness/assets/images/'+arr[index]['pic']+'&searchPic=false';
		$(this).attr('href',share_link);
		$(item).click(function(){
			var s=s_gi(s_account);
			s.linkTrackVars="events";
			s.linkTrackEvents="event23";
			s.events="event23";
		});
	});
	function zanHTML(data){
		var d = data['result'];
		$('.fc_list .score_txt').each(function(index,item){
			$(item).find('span.center').text(d[(index)]['count']);
		});
		$('.video_voit span').each(function(index,item){
			$(item).text(d[(index)]['count']);
		});
	};
	//http://www.fabiachina.com/api/index.php
	var getZan = $.ajax({
		type: 'GET',
		url: '../api/index.php/zan?'+new Date().getTime(),
		dataType: 'json',
		success: function(data){
			zanHTML(data);
		}
	})
	$('div.logo').each(function(index,item){
		$(item).find('a').click(function(e){
			var s=s_gi(s_account);
			s.linkTrackVars="events";
			s.linkTrackEvents="event"+(index+1);
			s.events="event"+(index+1);
			s.tl(this,'o','custom');
		});
	});
	$('.qlink_bar .qlink a').each(function(index,item){
		$(item).click(function(){
			var s=s_gi(s_account);
			s.linkTrackVars="events";
			s.linkTrackEvents="event"+(index+6);
			s.events="event"+(index+6);
			s.tl(this,'o','custom');
		});
	});
	$('#menu_list li a').each(function(index,item){
		$(item).click(function(){
			var s=s_gi(s_account);
			s.linkTrackVars="events";
			s.linkTrackEvents="event"+(index+12);
			s.events="event"+(index+12);
			s.tl(this,'o','custom');
		});
		
	});
	$('.tag_list').each(function(index,item){
		var num;
		if(index==0){
			num = 26;
		}else if(index==1){
			num = 32;
		}else if(index==2){
			num = 40;
		};
		$(item).find('li a').each(function(ind,itm){
			var num2;
			if(ind==0){
				num2 = 25;
			};
			$(itm).click(function(){
				var aa = num2 ? ind+num2 : ind+num;
				var s=s_gi(s_account);
				s.linkTrackVars="events";
				s.linkTrackEvents="event"+aa;
				s.events="event"+aa;
				s.tl(this,'o','custom');
			});
		});
	});
})(jQuery);